﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;
using System.Net;
using System.Data;
using RuleCode.Common;

namespace chatblog.usercate
{
    public partial class DynamicChat : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof (DynamicChat ));
            cbuser.dealcookie();
            if (AliApi.appUserId == string.Empty)
            {
                Response.Redirect("chatLogin.aspx?redirecturl="+Request.Url .ToString ());
            }
            
        }

        /// <summary>
        /// 解析数据库中保存的 聊天室的地址和打开时间
        /// </summary>
        /// <param name="chatAddress"></param>
        /// <returns></returns>
        private string[] resolveChatAddress(string chatAddress)
        {
            string[] chatAddressResult = new string[3];
            string[] arg0 = chatAddress.Split(";".ToCharArray());
            if (arg0.Length >= 3)
            {
                string[] ipArgs = arg0[0].Split("=".ToCharArray());
                chatAddressResult[0] = ipArgs[1];
                string[] timeArgs = arg0[1].Split("=".ToCharArray());
                chatAddressResult[1] = timeArgs[1];
                string[] hostArgs = arg0[2].Split("=".ToCharArray());
                chatAddressResult[2] = hostArgs[1];
            }
            return chatAddressResult;
        }


        [AjaxPro.AjaxMethod ]
        public string SetDynamicChatUserTos(string userto_recid)
        {
            string result = string.Empty ;
            if (AliApi.appUserId != string.Empty)
            {
                cbuser user = cbuser.Find("recid="+userto_recid );
                if (user.Count > 0)
                {
                    cbuser currentUser = cbuser.Find("cbuserid='"+AliApi.appUserId +"'");
                    if (currentUser.Count > 0)
                    {
                        if (currentUser.chatIsOnLine.Value == RuleCode.Enum.BooleanEnum.Yes) //在线
                        {
                            if (currentUser.cbChatAddress.Value != string.Empty)
                            {
                                string[] ipTimeHost = resolveChatAddress(currentUser.cbChatAddress.Value);
                                string hostName = HttpContext.Current.Request.UserHostName;// Dns.GetHostName();
                                string ip = HttpContext.Current.Request.UserHostAddress;// Dns.GetHostAddresses(hostName)[0].ToString();
                                if (ip == ipTimeHost[0] && hostName == ipTimeHost[2])
                                {
                                    if (user.cbuserid.Value != AliApi.appUserId)
                                    {
                                        currentUser.DynRoomUserTo_Recids.Value = "{[{recid:'" + userto_recid + "',userid:'" + user.cbuserid.Value + "'}]}";
                                        currentUser.Modify();
                                        //判断是否为陌生人发送消息
                                        string sqlText = " exec updateGroupRelationShip @user_to='" + user.cbuserid.Value + "',@user_from='" + AliApi.appUserId + "'";
                                        SqlHelper.Instance.ExecuteNonQuery(CommandType.Text, sqlText);
                                        result = "self";
                                    }
                                    else
                                    {
                                        result = "不能和自己聊天";
                                    }
                                }
                                else
                                {
                                    result = "对不起，您的当前用户已经在其他地方打开该聊天室！\n如果不是您自己的行为，请您及时修改密码，以防密码丢失！";
                                }
                            }
                            else
                            {
                                result = "聊天室状态异常";
                            }
                        }
                        else
                        {
                            if (user.cbuserid.Value != AliApi.appUserId)
                            {
                                currentUser.DynRoomUserTo_Recids.Value = "{[{recid:'" + userto_recid + "',userid:'" + user.cbuserid.Value + "'}]}";
                                currentUser.Modify();
                                //判断是否为陌生人发送消息
                                string sqlText = " exec updateGroupRelationShip @user_to='" + user.cbuserid.Value + "',@user_from='" + AliApi.appUserId + "'";
                                SqlHelper.Instance.ExecuteNonQuery(CommandType.Text, sqlText);
                                result = string.Empty;
                            }
                            else
                            {
                                result = "不能和自己聊天";
                            }
                        }
                    }
                    else
                    {
                        result = "当前用户不存在！";
                    }
                }
                else
                {
                    result = "未找到您聊天的用户！";
                }
            }
            else
            {
                result = "您还未登录！";
            }
            return result;
        }

    }
}
